package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.model.etl.FieldRemoveConfigJson;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  初始化 字段值清除  的Stage的配置
 *  stage的完整描述示例如：
 ""configuration" : [ {
 "name" : "filterOperation",
 "value" : "REMOVE"
 }, {
 "name" : "fields",
 "value" : [ "/remark", "/is_deleted", "/created_by", "/created_dt", "/updated_by", "/updated_dt", "/version" ]
 },....]
 *
 * @author wengzhonghui
 * @date 2020/9/16 10:32
 */
public class InitFieldRemoveConfig {

    /*
     * stage instanceName名称
     */
    private static final String STAGE_NAME = "g1_05";

    /*
     * 名称
     */
    private static final String CONFIG_NAME_REMOVE = "fields";
    private static final String CONFIG_NAME_OPERATION = "filterOperation";


    public static boolean init(StageConfigurationJson stage, FieldRemoveConfigJson configDto){
        if(STAGE_NAME.equalsIgnoreCase(stage.getInstanceName())) {
            if (configDto != null) {
                List<ConfigConfigurationJson> configs = intConfig(stage.getConfiguration(), configDto);
                stage.setConfiguration(configs);
            } else {
                List<ConfigConfigurationJson> configs =
                    stage.getConfiguration().stream().filter(e -> "fields".equals(e.getName())|| "filterOperation".equals(e.getName())).collect(Collectors
                    .toList());
                stage.getConfiguration().removeAll(configs);
            }
        }
        return true;
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs, FieldRemoveConfigJson configDto){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initFieldRemoveConfigs(config, configDto);
            newConfigs.add(config);
        }
        return newConfigs;
    }


    /*
    * 把 列值屏蔽
    * 格式示例如：
"   configuration" : [ {
        "name" : "filterOperation",
        "value" : "REMOVE"
      }, {
        "name" : "fields",
        "value" : [ "/remark", "/is_deleted", "/created_by", "/created_dt", "/updated_by", "/updated_dt", "/version" ]
      }
    * @param [config 原来的配置, columnNames:需要替换为null的列名列表]
    * @return 返回新生成的配置
    */
    private static ConfigConfigurationJson initFieldRemoveConfigs(ConfigConfigurationJson config, FieldRemoveConfigJson configDto){
        if(CONFIG_NAME_REMOVE.equalsIgnoreCase(config.getName())){
            List<String> configValList = new ArrayList();
            if(configDto != null && configDto.getFieldToRemoveColumns() != null && configDto.getFieldToRemoveColumns().size()>0){
                List<String> sourceFields = configDto.getFieldToRemoveColumns();
                for(String field : sourceFields){
                    configValList.add("/" + field);
                }

            }
            config = new ConfigConfigurationJson(CONFIG_NAME_REMOVE,configValList);
        }
        if(CONFIG_NAME_OPERATION.equalsIgnoreCase(config.getName())){
            if(configDto != null && StringUtils.isNotEmpty(configDto.getFilterOperation())){
                config = new ConfigConfigurationJson(CONFIG_NAME_OPERATION, configDto.getFilterOperation());
            }
        }
        return config;
    }


}
